home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / nktools.zip / DOS4TO5.PAS < prev    next >
Pascal/Delphi Source File  |  1990-06-18  |  20KB  |  456 lines

  1. UNIT Dos4to5;
  2. (*====================================================================*\
  3. || MODULE NAME:  Dos4to5                                              ||
  4. || DEPENDENCIES: System.TPU, Dos.TPU, StrUtil.TPU                     ||
  5. || LAST MOD ON:  8908.31                                              ||
  6. || PROGRAMMER:   Naoto Kimura                                         ||
  7. ||                                                                    ||
  8. || DESCRIPTION:  This is a library of DOS service routines for use    ||
  9. ||               with version 4.0 of the Turbo pascal compiler.  This ||
  10. ||               library of routines implements routines available in ||
  11. ||               version 5.0 of the Turbo pascal compiler that are    ||
  12. ||               unavailable in version 4.0.                          ||
  13. ||                                                                    ||
  14. || Modification history                                               ||
  15. ||                                                                    ||
  16. || 9006.18    Naoto Kimura                                            ||
  17. ||            * Recoded some of the routines in assembler for speed.  ||
  18. \*====================================================================*)
  19.  
  20. {$S+}    {Stack checking on}
  21. {$I-}    {I/O checking off}
  22. {$N-}    {No numeric coprocessor}
  23.  
  24. INTERFACE
  25.  
  26. USES
  27.     Dos,StrUtil;
  28.  
  29. (*--------------------------------------------------------------------*\
  30. | NAME: DosVersion                                                     |
  31. |                                                                      |
  32. |     This function returns the version of DOS installed on the        |
  33. | computer.  The low order byte of the word returned contains the      |
  34. | major version number, while the high order byte contains the minor   |
  35. | version number.                                                      |
  36. \*--------------------------------------------------------------------*)
  37. FUNCTION DosVersion : Word;
  38.  
  39. (*--------------------------------------------------------------------*\
  40. | NAME: GetCBreak                                                      |
  41. |                                                                      |
  42. |     This procedure returns the Control-break checking status of DOS. |
  43. \*--------------------------------------------------------------------*)
  44. PROCEDURE GetCBreak (VAR Break : Boolean);
  45.  
  46. (*--------------------------------------------------------------------*\
  47. | NAME: SetCBreak                                                      |
  48. |                                                                      |
  49. |     This procedure sets the Control-break checking status of DOS.    |
  50. \*--------------------------------------------------------------------*)
  51. PROCEDURE SetCBreak (Break : Boolean);
  52.  
  53. (*--------------------------------------------------------------------*\
  54. | NAME: GetVerify                                                      |
  55. |                                                                      |
  56. |     This procedure sets the Control-break checking status of DOS.    |
  57. \*--------------------------------------------------------------------*)
  58. PROCEDURE GetVerify (var Verify : Boolean);
  59.  
  60. (*--------------------------------------------------------------------*\
  61. | NAME: SetVerify                                                      |
  62. |                                                                      |
  63. |     This procedure sets the Control-break checking status of DOS.    |
  64. \*--------------------------------------------------------------------*)
  65. PROCEDURE SetVerify (Verify : Boolean);
  66.  
  67. TYPE
  68.     ComStr    = string[127];
  69.     PathStr    = string[79];
  70.     DirStr    = string[67];
  71.     NameStr    = string[8];
  72.     ExtStr    = string[4];
  73.  
  74. (*--------------------------------------------------------------------*\
  75. | NAME: FSearch                                                        |
  76. |                                                                      |
  77. |     This function is used to search for the specified file in a      |
  78. | given set of directories.  The Path parameter is formatted in the    |
  79. | same manner in which the DOS environment variable PATH is formatted  |
  80. | (each entry is separated from the next with a semicolon).            |
  81. \*--------------------------------------------------------------------*)
  82. FUNCTION FSearch(
  83.         Path    : PathStr;
  84.         DirList    : String
  85.     ) : PathStr;
  86.  
  87. (*--------------------------------------------------------------------*\
  88. | NAME: FSplit                                                         |
  89. |                                                                      |
  90. |     This procedure splits a fully specified file name, and splits    |
  91. | the filename into its components.                                    |
  92. \*--------------------------------------------------------------------*)
  93. PROCEDURE FSplit (
  94.         Path    : PathStr;
  95.     VAR Dir        : DirStr;
  96.     VAR Name    : NameStr;
  97.     VAR Ext        : ExtStr  );
  98.  
  99. (*--------------------------------------------------------------------*\
  100. | NAME: FExpand                                                        |
  101. |                                                                      |
  102. |     This function expands the file name to a fully qualified path    |
  103. | file name.                                                           |
  104. \*--------------------------------------------------------------------*)
  105. FUNCTION FExpand (
  106.         Path : PathStr
  107.     ) : PathStr;
  108.  
  109. (*--------------------------------------------------------------------*\
  110. | NAME:  GetEnv                                                        |
  111. |                                                                      |
  112. |     This routine is patterned after the UNIX operating system call   |
  113. | which obtains the value of a specified environment variable.  A      |
  114. | process will inherit a copy of the parent's environment.  Often, the |
  115. | environment variables are used to communicate between processes.     |
  116. | Here are some examples of the usage of this function:                |
  117. |                                                                      |
  118. |     s := GetEnv('PATH'))          -- Sets "s" to the list of         |
  119. |                                      directories in which executable |
  120. |                                      programs are to be found.       |
  121. |     writeln(GetEnv('PROMPT'))     -- Prints the value of the DOS     |
  122. |                                      command interpreter prompt.     |
  123. |     s := GetEnv('INITFILE')       -- Sets "s" to the value of the    |
  124. |                                      environment variable "FOO".     |
  125. \*--------------------------------------------------------------------*)
  126. FUNCTION GetEnv( envvar : string ) : string;
  127.  
  128. (*--------------------------------------------------------------------*\
  129. | NAME:  EnvCount                                                      |
  130. |                                                                      |
  131. |     This function returns the number of environment strings set in   |
  132. | the environment.                                                     |
  133. \*--------------------------------------------------------------------*)
  134. FUNCTION EnvCount : integer;
  135.  
  136. (*--------------------------------------------------------------------*\
  137. | NAME:  EnvStr                                                        |
  138. |                                                                      |
  139. |     This function returns the Index'th environment string.  The      |
  140. | string returned by this function is of the form 'VAR=VALUE.'  If     |
  141. | Index is beyond the last environment, then it will return a null     |
  142. | string.                                                              |
  143. \*--------------------------------------------------------------------*)
  144. FUNCTION EnvStr( Index : integer ) : string;
  145.  
  146. IMPLEMENTATION
  147.  
  148. CONST
  149.     DirSeparator    = '\';
  150.     AltDirSeparator    = '/';
  151.     DskSeparator    = ':';
  152.     DirCharSet        : CharSet    = ['/','\'];
  153.     DOSsepChars        : CharSet    = ['/','\',':'];
  154.  
  155. TYPE
  156.     (*----------------------------------------------------------------*\
  157.     | The following record type describes the contents of the Program  |
  158.     | Segment Prefix (PSP).                                            |
  159.     |                                                                  |
  160.     |    int20H            exit code                              |
  161.     |    TopOfMemory        Memory size in paragraphs              |
  162.     |    Reserved0        ??? (0)                                |
  163.     |    PSP_DOS        Far call to DOS                        |
  164.     |    TerminationAddr    Terminate address                      |
  165.     |    BreakExitAddr        Address of break handler               |
  166.     |    CriticalErrorAddr    Address of critical error handler      |
  167.     |    ParentPSP_Seg        Parent PSP segment                     |
  168.     |    OpenFiles        Open files, $ff = unused               |
  169.     |    EnvironmentSeg        Environment segment                    |
  170.     |    PSP_OldStack        far pointer to processes SS:SP ???     |
  171.     |    PSP_Nfiles        maximum open files                     |
  172.     |    PSP_aofile        ofile address                          |
  173.     |    Reserved3        Unused ???                             |
  174.     |    PSP_int21        INT 21, far return                     |
  175.     |    Reserved4        Unused ???                             |
  176.     |    PSP_FCB1ext        FCB #1 extension                       |
  177.     |    PSP_FCB1        FCB #1                                 |
  178.     |    PSP_FCB2ext        FCB #2 extension                       |
  179.     |    PSP_FCB2        FCB #2                                 |
  180.     |    PSP_DMA        Command Tail                           |
  181.     |                                                                  |
  182.     \*----------------------------------------------------------------*)
  183.     PSPtype    = RECORD
  184.         int20H        : word;                {00}
  185.         TopOfMemory        : word;                {02}
  186.         Reserved0        : byte;                {04}
  187.         PSP_DOS        : ARRAY [0..4] OF byte;        {05}
  188.         TerminationAddr,                    {0A}
  189.         BreakExitAddr,                    {0E}
  190.         CriticalErrorAddr    : pointer;            {12}
  191.         ParentPSP_Seg    : word;                {16}
  192.         OpenFiles        : ARRAY [0..19] OF byte;    {18}
  193.         EnvironmentSeg    : word;                {2C}
  194.         PSP_OldStack    : pointer;            {2E}
  195.         PSP_Nfiles        : integer;            {32}
  196.         PSP_aofile        : pointer;            {34}
  197.         Reserved3        : ARRAY [0..23] OF byte;    {38}
  198.         PSP_int21        : ARRAY [0..1] OF byte;        {50}
  199.         Reserved4        : ARRAY [0..1] OF byte;        {53}
  200.         PSP_FCB1ext        : ARRAY [0..6] OF byte;        {55}
  201.         PSP_FCB1        : ARRAY [0..8] OF byte;        {5C}
  202.         PSP_FCB2ext        : ARRAY [0..6] OF byte;        {65}
  203.         PSP_FCB2        : ARRAY [0..19] OF byte;    {6C}
  204.         PSP_DMA        : ARRAY [0..127] OF byte    {80}
  205.     END;
  206.  
  207. {$L Dos4to5.OBJ}
  208.  
  209. (*--------------------------------------------------------------------*\
  210. | NAME: DosVersion                                                     |
  211. \*--------------------------------------------------------------------*)
  212. FUNCTION DosVersion : Word;
  213.     External;
  214.  
  215. (*--------------------------------------------------------------------*\
  216. | NAME: GetCBreak                                                      |
  217. \*--------------------------------------------------------------------*)
  218. PROCEDURE GetCBreak (VAR Break : Boolean);
  219.     External;
  220.  
  221. (*--------------------------------------------------------------------*\
  222. | NAME: SetCBreak                                                      |
  223. \*--------------------------------------------------------------------*)
  224. PROCEDURE SetCBreak (Break : Boolean);
  225.     External;
  226.  
  227. (*--------------------------------------------------------------------*\
  228. | NAME: GetVerify                                                      |
  229. \*--------------------------------------------------------------------*)
  230. PROCEDURE GetVerify (VAR Verify : Boolean);
  231.     External;
  232.  
  233. (*--------------------------------------------------------------------*\
  234. | NAME: SetVerify                                                      |
  235. \*--------------------------------------------------------------------*)
  236. PROCEDURE SetVerify (Verify : Boolean);
  237.     External;
  238.  
  239. (*--------------------------------------------------------------------*\
  240. | NAME: FSearch                                                        |
  241. \*--------------------------------------------------------------------*)
  242. FUNCTION FSearch(
  243.         Path    : PathStr;
  244.         DirList    : String
  245.     ) : PathStr;
  246.     VAR
  247.     Found    : Boolean;
  248.     Tmp    : String;
  249.     i    : Integer;
  250.     f    : Text;
  251.     BEGIN
  252.     Found := FALSE;
  253.     Assign(f,Path);
  254.     {$I-}Reset(f);{$I+}
  255.     IF IOresult=0 THEN BEGIN
  256.         Found := TRUE;
  257.         Close(f);
  258.         Tmp := Path
  259.       END;
  260.     WHILE (DirList <> '') AND NOT FOUND DO BEGIN
  261.         i := Pos(';',DirList);
  262.         IF i=0 THEN
  263.         i := Length(DirList)+1;
  264.         Tmp := Copy(DirList,1,i-1);
  265.         DirList := Copy(DirList,i+1,Length(DirList)-i);
  266.         IF Tmp[Length(Tmp)] IN ['/','\',':'] THEN
  267.         Tmp := Tmp+Path
  268.         ELSE
  269.         Tmp := Tmp+'\'+Path;
  270.         Assign(f,Tmp);
  271.         {$I-}Reset(f);{$I+}
  272.         IF IOresult=0 THEN BEGIN
  273.         Found := TRUE;
  274.         close(f)
  275.           END
  276.       END;
  277.     IF Found THEN
  278.         FSearch := Tmp
  279.     ELSE
  280.         FSearch := ''
  281.     END;    (* FSearch *)
  282.  
  283. (*--------------------------------------------------------------------*\
  284. | NAME: FSplit                                                         |
  285. |                                                                      |
  286. | EXTERNALS:                                                           |
  287. |     const      DirSeparator, AltDirSeparator     (local to unit)     |
  288. |     function   RCharSetPos                       (from StrUtil unit) |
  289. \*--------------------------------------------------------------------*)
  290. PROCEDURE FSplit (
  291.         Path    : PathStr;
  292.     VAR Dir        : DirStr;
  293.     VAR Name    : NameStr;
  294.     VAR Ext        : ExtStr  );
  295.     VAR
  296.     i,j    : integer;
  297.     BEGIN
  298.     i := RCharSetPos(DOSsepChars,Path);
  299.     IF i=0 THEN
  300.         Dir := ''
  301.     ELSE BEGIN
  302.         Dir := Copy(Path,1,i);
  303.         Delete(Path,1,i)
  304.       END;
  305.     j := RPos('.',Path);
  306.     IF j=0 THEN BEGIN
  307.         Name := Path;
  308.         Ext := ''
  309.       END
  310.     ELSE BEGIN
  311.         Name := copy(Path,1,j-1);
  312.         Ext := copy(Path,j,length(Path)-j+1)
  313.       END
  314.     END;    (* FSplit *)
  315.  
  316. (*--------------------------------------------------------------------*\
  317. | NAME: FExpand                                                        |
  318. |                                                                      |
  319. | EXTERNALS:                                                           |
  320. |     const      DirSeparator, AltDirSeparator     (local to unit)     |
  321. |     function   RCharSetPos                       (from StrUtil unit) |
  322. \*--------------------------------------------------------------------*)
  323. FUNCTION FExpand (
  324.         Path : PathStr
  325.     ) : PathStr;
  326.     VAR
  327.     i,j        : integer;
  328.     TmpStr,
  329.     WorkBuffer    : string;
  330.     BEGIN
  331.     TmpStr := Path;
  332.     (* strip off any drivespec and get pwd on drive *)
  333.     IF Pos(DskSeparator,TmpStr) <> 2 THEN
  334.         GetDir(0,WorkBuffer)
  335.     ELSE IF NOT (Path[1] IN Alphabet) THEN
  336.         GetDir(0,WorkBuffer)
  337.     ELSE BEGIN
  338.         GetDir(ord(UpCase(TmpStr[1]))-ord('A')+1, WorkBuffer);
  339.         TmpStr := copy(TmpStr,3,length(TmpStr)-2)
  340.       END;
  341.  
  342.     (* strip trailing slash on pwd of selected drive *)
  343.     IF length(WorkBuffer) > 0 THEN
  344.         IF WorkBuffer[length(WorkBuffer)] IN DirCharSet THEN
  345.         Dec(WorkBuffer[0]);
  346.  
  347.     (* handle reference to root *)
  348.     IF TmpStr[1] IN DirCharSet THEN BEGIN
  349.         WorkBuffer[0] := #2;
  350.         WHILE (length(TmpStr)>0) AND (TmpStr[1] IN DirCharSet) DO
  351.         TmpStr := copy(TmpStr,2,length(TmpStr)-1)
  352.       END;
  353.  
  354.     (* Strip relative refereces *)
  355.     i := CharSetPos(DirCharSet,TmpStr);
  356.     WHILE i <> 0 DO BEGIN
  357.         IF copy(TmpStr,1,i-1)='.' THEN
  358.         TmpStr := copy(TmpStr,3,length(TmpStr)-2)
  359.         ELSE IF copy(TmpStr,1,i-1)='..' THEN BEGIN
  360.         TmpStr := copy(TmpStr,4,length(TmpStr)-2);
  361.         j := RCharSetPos(DirCharSet,WorkBuffer);
  362.         IF j>0 THEN
  363.             Dec(WorkBuffer[0],length(WorkBuffer)-j+1)
  364.           END
  365.         ELSE BEGIN
  366.         WorkBuffer := WorkBuffer + DirSeparator
  367.                 + copy(TmpStr,1,i-1);
  368.         TmpStr := copy(TmpStr,i+1,length(TmpStr)-i)
  369.           END;
  370.         i := CharSetPos(DirCharSet,TmpStr)
  371.       END;
  372.     IF TmpStr = '.' THEN
  373.         FExpand := WorkBuffer
  374.     ELSE IF TmpStr <> '..' THEN
  375.         FExpand := WorkBuffer + DirSeparator + TmpStr
  376.     ELSE BEGIN
  377.         j := RCharSetPos(DirCharSet,WorkBuffer);
  378.         IF j = 0 THEN
  379.         FExpand := WorkBuffer + DirSeparator
  380.         ELSE BEGIN
  381.         IF j > 3 THEN
  382.             Dec(WorkBuffer[0],length(WorkBuffer)-j+1)
  383.         ELSE
  384.             Dec(WorkBuffer[0],length(WorkBUffer)-j);
  385.         FExpand := WorkBuffer
  386.           END
  387.       END
  388.     END;    (* FExpand *)
  389.  
  390. CONST
  391.     EnvironmentSeg    : word    = 0;
  392.  
  393. (*--------------------------------------------------------------------*\
  394. | NAME:  GetEnv                                                        |
  395. |                                                                      |
  396. | EXTERNALS:                                                           |
  397. |     word        EnvironmentSeg   (local to unit)                     |
  398. \*--------------------------------------------------------------------*)
  399. FUNCTION GetEnv( envvar : string ) : string;
  400.     VAR
  401.     i        : integer;
  402.     found        : boolean;
  403.     WorkBuffer    : string;
  404.     BEGIN  (* GetEnv *)
  405.     i := 0;
  406.     found := false;
  407.     WHILE NOT (found OR (mem[EnvironmentSeg:i]=0)) DO BEGIN
  408.         WorkBuffer := '';
  409.         WHILE mem[EnvironmentSeg:i] <> ord('=') DO BEGIN
  410.         WorkBuffer := WorkBuffer + chr(mem[EnvironmentSeg:i]);
  411.         Inc(i)
  412.           END;
  413.         Inc(i);        (* skip '=' *)
  414.         found := WorkBuffer = envvar;
  415.         WorkBuffer := '';
  416.         WHILE mem[EnvironmentSeg:i] <> 0 DO BEGIN
  417.         WorkBuffer := WorkBuffer + chr(mem[EnvironmentSeg:i]);
  418.         Inc(i)
  419.           END;
  420.         Inc(i)        (* skip '\0' *)
  421.       END;
  422.     IF found THEN
  423.         GetEnv := WorkBuffer
  424.     ELSE
  425.         GetEnv := ''
  426.     END;  (* GetEnv *)
  427.  
  428. (*--------------------------------------------------------------------*\
  429. | NAME:  EnvCount                                                      |
  430. |                                                                      |
  431. | EXTERNALS:                                                           |
  432. |     word        EnvironmentSeg   (local to unit)                     |
  433. \*--------------------------------------------------------------------*)
  434. FUNCTION EnvCount : integer;
  435.     External;
  436.  
  437. (*--------------------------------------------------------------------*\
  438. | NAME:  EnvStr                                                        |
  439. |                                                                      |
  440. | EXTERNALS:                                                           |
  441. |     word        EnvironmentSeg   (local to unit)                     |
  442. \*--------------------------------------------------------------------*)
  443. FUNCTION EnvStr( Index : integer ) : string;
  444.     External;
  445.  
  446. (*====================================================================*\
  447. ||                  Dos4to5 unit initialization code                  ||
  448. ||--------------------------------------------------------------------||
  449. || EXTERNALS:                                                         ||
  450. ||     function  PrefixSeg                                            ||
  451. ||     type      PSPtype                                              ||
  452. \*====================================================================*)
  453. BEGIN
  454.     EnvironmentSeg := PSPtype(ptr(PrefixSeg,$0)^).EnvironmentSeg
  455. END.
  456.